/**
 * Copyright (c) 2011-2016, James Zhan 詹波 (jfinal@126.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jfinal.plugin.activerecord;

import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;

import com.jfinal.plugin.activerecord.dialect.Dialect;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;

/**
 * DbKit
 * 
 * 通过 name管理数据库配置文件，管理各个 Model对应的配置文件
 */
@SuppressWarnings("rawtypes")
public final class DbKit {

	/**
	 * The main Config object for system
	 */
	static Config config = null;

	private static Map<Class<? extends Model>, Config> modelToConfig = new HashMap<Class<? extends Model>, Config>();
	private static Map<String, Config> configNameToConfig = new HashMap<String, Config>();

	static final Object[] NULL_PARA_ARRAY = new Object[0];
	public static final String MAIN_CONFIG_NAME = "main";
	public static final int DEFAULT_TRANSACTION_LEVEL = Connection.TRANSACTION_REPEATABLE_READ;
	public static final Dialect DEFAULT_DIALECT = new MysqlDialect();
	public static final IContainerFactory DEFAULT_CONTAINER_FACTORY = IContainerFactory.defaultContainerFactory;

	private DbKit() {
	}

	/**
	 * Add Config object
	 * @param config the Config contains DataSource, Dialect and so on
	 */
	public static void addConfig(Config config) {
		if (config == null)
			throw new IllegalArgumentException("Config can not be null");
		if (configNameToConfig.containsKey(config.name))
			throw new IllegalArgumentException("Config already exists: " + config.name);

		configNameToConfig.put(config.name, config);

		/** 
		 * Replace the main config if current config name is MAIN_CONFIG_NAME
		 */
		if (MAIN_CONFIG_NAME.equals(config.name))
			DbKit.config = config;

		/**
		 * The configName may not be MAIN_CONFIG_NAME,
		 * the main config have to set the first comming Config if it is null
		 */
		if (DbKit.config == null)
			DbKit.config = config;
	}

	static void addModelToConfigMapping(Class<? extends Model> modelClass, Config config) {
		modelToConfig.put(modelClass, config);
	}

	public static Config getConfig() {
		return config;
	}

	public static Config getConfig(String configName) {
		return configNameToConfig.get(configName);
	}

	public static Config getConfig(Class<? extends Model> modelClass) {
		return modelToConfig.get(modelClass);
	}

	public static Config removeConfig(String configName) {
		if (DbKit.config != null && DbKit.config.name.equals(configName))
			throw new RuntimeException("Can not remove the main config.");

		return configNameToConfig.remove(configName);
	}
}
